<%@page import="support.*, java.util.*, java.sql.*" %>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style>
<!-- Three column style settings -->
#left {
  margin-right: 153px;
}
#right {
  margin-left: 150px;
}

#left_wrapper {
  width: 50%;
  margin-right: -150px;
  float: left;
  position: relative;
  z-index: 0;
}

#middle_wrapper {
  width: 300px;
  margin-right: -150px;
  float: left;
  position: relative;
  z-index: 2;
}

#right_wrapper {
  width: 49.9%;
  float: left;
  position: relative;
  z-index: 1;
}
</style>
<title>Degree - Choose Location</title>
</head>
<body>
    <!--  Book keeping for degree.jsp -->
    <%             
	      // if coming from address.jsp for the first time
	   if(session.getAttribute("residence").equals("United States") && session.getAttribute("degreeFlag").equals("false")){
	      // gets address infomation from previous page
	      String address = request.getParameter("address"); 
	      session.setAttribute("address", address);
	      String city = request.getParameter("city"); 
	      session.setAttribute("city", city);
	      String state = request.getParameter("state"); 
	      session.setAttribute("state", state);
	      String zip = request.getParameter("zip"); 
	      session.setAttribute("zip", zip);
	      String area = request.getParameter("area"); 
	      session.setAttribute("area", area);
	      String number = request.getParameter("number"); 
	      session.setAttribute("number", number);
          // makes US resident domestic
          String domestic = "domestic"; 
          session.setAttribute("domestic", domestic);
          // locks from altering the address anymore
          session.setAttribute("degreeFlag","true");
      }else{
          // if coming from residency.jsp
          // keeps track of domestic/nondomestic
          String domestic = request.getParameter("domestic"); 
          // sets domestic as session attribute
          session.setAttribute("domestic", domestic);
      }
    
      // make HashLinkedSet to hold this current degree
      Set currentDegree = new LinkedHashSet<String>();
      session.setAttribute("currentDegree", currentDegree);
    %>
    
    <!--  Hidden object to keep track of college location -->
    <input type="hidden" name="degreeLocation" value="">
    Provide location of University <br> <br>
    
    <%
    // Connect to Database
    try {
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    
    // connect to local host 8082
    Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:8082/CSE135?" + "user=postgres&password=postgres");
    // create SQL statement
    Statement stmt = conn.createStatement();
    
    ResultSet rset = stmt.executeQuery("SELECT COUNT(distinct loc) FROM universities");
    int counter = 0;  // keeps track of the university we are on
    int universitySize = 0;  // keeps track of the size of the table
    if(rset.next()){
        universitySize = rset.getInt(1);
    }
    %>
    
    <%
    // use sql statement to display all the disciplines
    rset = stmt.executeQuery("SELECT loc FROM universities GROUP BY loc ORDER BY loc");
   
    %>
    
    <!-- Create the LEFT COLUMN -->
    <div id="left_wrapper">
    <div id="left">
    <%
     for(int i=0; i<universitySize/3; i++, counter++){
       // use hidden object to make href
       if(rset.next()){
         out.print(
               "<a href=\"university.jsp?degreeLocation=" + rset.getString(1)
               + "\">" + rset.getString(1) + "</a>" + "<br>");     
       }
     }
    %>
    </div>
    </div>
    
     <!-- Create the MIDDLE COLUMN -->
    <div id="middle_wrapper">
    <div id="middle">
    <%
     for(int i =0;i<universitySize/3; i++, counter++){
         // use hidden object to make href
         if(rset.next()){
           out.print(
                 "<a href=\"university.jsp?degreeLocation=" + rset.getString(1)
                 + "\">" + rset.getString(1) + "</a>" + "<br>");    
         }
     }
    %>
    </div>
    </div>
    
     <!-- Create the RIGHT COLUMN -->
    <div id="right_wrapper">
    <div id="right">
    <%
     for(; counter<universitySize; counter++){
         // use hidden object to make href
         if(rset.next()){
           out.print(
                 "<a href=\"university.jsp?degreeLocation=" + rset.getString(1)
                 + "\">" + rset.getString(1) + "</a>" + "<br>");  
         }
      }
    %>
    </div>
    </div>


</body>
</html>